//
//  WithdrawViewController.m
//  PigMarket
//
//  Created by mac on 2019/5/28.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "WithdrawViewController.h"
#import "TopUpAndWithdrawView.h"
#import "WithdrawDetailViewController.h"
#import "AccountBoundViewController.h"
#import "InputAlipayAccountViewController.h"

@interface WithdrawViewController ()
/**
 充值view
 */
@property (nonatomic , strong) TopUpAndWithdrawView *withdrawView;
/**
 充值方式
 */
@property (nonatomic , assign) PayType payType;
/**
 已绑定的微信账户
 */
@property (nonatomic , strong) NSString *wechatBoundAccount;
@end

@implementation WithdrawViewController

#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"提现";
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    //取出微信openid
    self.wechatBoundAccount = [UserInfoDataProvider sharedInstance].userInfo.withdrawWx;
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.withdrawView];
}

- (void) initLayout {
    [self.withdrawView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void) initAction {
    @weakify(self);
    [self.withdrawView setWechatBlock:^{
        @strongify(self);
        self.withdrawView.wechatSelectImg.image = [UIImage imageNamed:@"pay_default_selected"];
        self.withdrawView.alipaySelectImg.image = [UIImage imageNamed:@"pay_default"];
        self.payType = PayTypeWechat;
        [self checkBoundWechatAccount];
    }];
    [self.withdrawView setAlipayBlock:^{
        @strongify(self);
        self.withdrawView.wechatSelectImg.image = [UIImage imageNamed:@"pay_default"];
        self.withdrawView.alipaySelectImg.image = [UIImage imageNamed:@"pay_default_selected"];
        self.payType = PayTypeAlipay;
    }];
    
    [self.withdrawView.confirmBtn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
- (void)initData {
//    self.withdrawView.wechatAccount.text = [NSString stringWithFormat:@"账户:%@",self.wechatBoundAccount];
    self.withdrawView.title.text = @"提取金额";
    self.withdrawView.payTypeTitle.text = @"选择提现方式";
    self.withdrawView.withdrawMoney.text = [NSString stringWithFormat:@"可提取金额为%@",self.balance];
    [self.withdrawView.confirmBtn setTitle:@"预计两小时内到账，确认提现" forState:UIControlStateNormal];
}
- (void)confirmBtnClick {
    NSString *money = self.withdrawView.moneyInputField.text;
    if ([JTool iSNull:money]) {
        [self toastWithContent:@"请输入提现金额"];
        return;
    }
    if ([money intValue] > [self.balance intValue]) {
        [self toastWithContent:@"提现金额大于可用余额"];
        return;
    }
    if (!self.payType) {
        [self toastWithContent:@"请选择提现方式"];
        return;
    }
    switch (self.payType) {
        case PayTypeWechat:
        {
            [[PayInputView sharedInstance] display];
            @weakify(self);
            [[PayInputView sharedInstance] setInputCompleteBlock:^(SecureTextEntryView * _Nonnull secureView) {
                @strongify(self);
                [self withdrawRequestWithMoney:money payPwd:secureView.text];
            }];
        }
            break;
        case PayTypeAlipay:
        {
            InputAlipayAccountViewController *vc = [[InputAlipayAccountViewController alloc] init];
            vc.withdrawMoney = money;
            [self.navigationController pushViewController:vc animated:YES];
            
        }
            break;
        default:
            break;
    }
    
}
- (void)withdrawRequestWithMoney:(NSString *)money payPwd:(NSString *)payPwd{

    @weakify(self);
    NSNumber *memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    [[[PigMarketApi sharedInstance] withdrawWithMemberId:memberId money:money payPwd:payPwd] subscribeNext:^(NSNull * _Nullable x) {
        @strongify(self);
        WithdrawDetailViewController *vc = [[WithdrawDetailViewController alloc] init];
        vc.withdrawMoney = money;
        vc.account = [NSString stringWithFormat:@"微信%@",@"账户号"];
        [self.navigationController pushViewController:vc animated:YES];
    } error:^(NSError * _Nullable error) {
        
        @strongify(self);
        [self handlingError:error];
        [[PayInputView sharedInstance] unDisplay];
    }];
}
- (void)checkBoundWechatAccount {
    if ([JTool iSNull:self.wechatBoundAccount]) {
        @weakify(self);
        [[AlertAction sharedInstance] showAlertWithTitle:nil message:@"暂无绑定微信，是否去绑定" confirmBtnTitle:@"去绑定" confirmBlock:^{
            @strongify(self);
            [self.navigationController pushViewController:[[AccountBoundViewController alloc] init] animated:YES];
        } cancelBlock:nil];
    }
}
#pragma mark - getter and setter
- (TopUpAndWithdrawView *)withdrawView {
    if (!_withdrawView) {
        _withdrawView = [[TopUpAndWithdrawView alloc] initWithType:@"提现"];
    }
    return _withdrawView;
}
@end
